<template>
  <div class="home-container">
    <!-- 套餐统计分析 -->
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>套餐统计分析</span>
        </div>
      </template>
      <div class="chart-container">
        <div v-for="item in homeData.broadbandCountVOList" :key="item.broadbandName" class="stat-item">
          <div class="stat-name">{{ item.broadbandName }}</div>
          <div class="stat-value">{{ item.count }}</div>
        </div>
      </div>
    </el-card>

    <!-- 宽带统计分析 -->
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>宽带统计分析</span>
        </div>
      </template>
      <div class="stat-container">
        <div class="stat-item">
          <div class="stat-name">已安装宽带数量</div>
          <div class="stat-value">{{ homeData.installedBroadbands }}</div>
        </div>
      </div>
    </el-card>

    <!-- 用户等级统计分析 -->
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>用户等级统计分析</span>
        </div>
      </template>
      <div class="chart-container">
        <div class="stat-item">
          <div class="stat-name">普通用户</div>
          <div class="stat-value">{{ homeData.userCountVO?.userCount }}</div>
        </div>
        <div class="stat-item">
          <div class="stat-name">VIP用户</div>
          <div class="stat-value">{{ homeData.userCountVO?.vipCount }}</div>
        </div>
        <div class="stat-item">
          <div class="stat-name">SVIP用户</div>
          <div class="stat-value">{{ homeData.userCountVO?.svipCount }}</div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { BroadbandApi } from '@/api/broadband'

interface BroadbandCountVO {
  broadbandName: string
  count: number
}

interface UserCountVO {
  userCount: number
  vipCount: number
  svipCount: number
}

interface HomeDataVO {
  broadbandCountVOList: BroadbandCountVO[]
  installedBroadbands: number
  userCountVO: UserCountVO
}

const homeData = ref<HomeDataVO>({
  broadbandCountVOList: [],
  installedBroadbands: 0,
  userCountVO: {
    userCount: 0,
    vipCount: 0,
    svipCount: 0
  }
})

// 获取首页数据
const getHomeData = async () => {
  try {
    const data = await BroadbandApi.getHomeData()
    homeData.value = data
  } catch (error) {
    console.error('获取首页数据失败:', error)
  }
}

onMounted(() => {
  getHomeData()
})
</script>

<style lang="scss" scoped>
.home-container {
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.box-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .chart-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 20px;
    padding: 10px;
  }

  .stat-container {
    padding: 10px;
  }

  .stat-item {
    text-align: center;
    padding: 15px;
    background-color: var(--el-color-primary-light-9);
    border-radius: 8px;

    .stat-name {
      font-size: 14px;
      color: var(--el-text-color-secondary);
      margin-bottom: 8px;
    }

    .stat-value {
      font-size: 24px;
      font-weight: bold;
      color: var(--el-color-primary);
    }
  }
}
</style>
